package com.zwj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zwj.bo.CourseNodeBo;
import com.zwj.dto.PaginationDate;
import com.zwj.dto.Result;
import com.zwj.dto.ReturnDate;
import com.zwj.entity.CourseInfoEntity;
import com.zwj.entity.CourseNodeEntity;
import com.zwj.entity.SysMenuEntity;
import com.zwj.entity.SysUserEntity;
import com.zwj.enums.IsDeletedEnum;
import com.zwj.enums.SexEnum;
import com.zwj.enums.StatusEnum;
import com.zwj.mapper.CourseNodeMapper;
import com.zwj.mapper.SysUserMapper;
import com.zwj.mapper.UserRoleMapper;
import com.zwj.service.CourseNodeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwj.utils.DataCheckUtils;
import com.zwj.utils.DateUtils;
import com.zwj.utils.PaginationUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zwj
 * @since 2021-11-02
 */
@Service
public class CourseNodeServiceImpl extends ServiceImpl<CourseNodeMapper, CourseNodeEntity> implements CourseNodeService {
    private final CourseNodeMapper courseNodeMapper ;
    public CourseNodeServiceImpl(CourseNodeMapper courseNodeMapper) {
        this.courseNodeMapper = courseNodeMapper;
    }
    @Override
    public Result selectCourseNode(CourseNodeBo courseNodeBo) {
        QueryWrapper<CourseNodeEntity> sectionQueryWrapper = new QueryWrapper<>();
        sectionQueryWrapper.lambda().eq(CourseNodeEntity::getCourseId, courseNodeBo.getCourseId()).eq(CourseNodeEntity::getIsDeleted, IsDeletedEnum.NO.getKey()).orderByAsc(CourseNodeEntity::getSortCode);
        List<CourseNodeEntity> courseNodeEntities = courseNodeMapper.selectList(sectionQueryWrapper);
        if (courseNodeEntities.size() == 0) return Result.error().message("查询结果为空");
        return Result.ok().data(courseNodeEntities);
    }

    @Override
    public Result deleteCourseNode(CourseNodeBo courseNodeBo) {
        CourseNodeEntity courseNodeEntity = new CourseNodeEntity();
        courseNodeEntity.setIsDeleted(IsDeletedEnum.YES.getKey());
        UpdateWrapper<CourseNodeEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(CourseNodeEntity::getId, courseNodeBo.getId()).eq(CourseNodeEntity::getIsDeleted,IsDeletedEnum.NO.getKey());
        int row = courseNodeMapper.update(courseNodeEntity, updateWrapper);
        //判断修改结果
        if (row == 0) {
            return Result.error().message("删除章节失败");
        }
        return Result.ok();
    }

    @Override
    public Result addCourseNode(CourseNodeEntity courseNodeEntity, String userId) {
        courseNodeEntity.setIsDeleted(IsDeletedEnum.NO.getKey());
        courseNodeEntity.setCreateBy(userId);
        courseNodeEntity.setCreateTime(DateUtils.getCurrentTime());
        int row = courseNodeMapper.insert(courseNodeEntity);
        //判断插入结果
        if (row == 0) {
            return Result.error().message("添加章节失败");
        }
        return Result.ok();
    }

    @Override
    public Result updateCourseNode(CourseNodeEntity courseNodeEntity, String userId) {
        courseNodeEntity.setUpdateBy(userId);
        courseNodeEntity.setUpdateTime(DateUtils.getCurrentTime());
        UpdateWrapper<CourseNodeEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(CourseNodeEntity::getId, courseNodeEntity.getId());
        int row = courseNodeMapper.update(courseNodeEntity, updateWrapper);
        //判断修改结果
        if (row == 0) {
            return Result.error().message("编辑章节信息失败");
        }
        return Result.ok();
    }

    @Override
    public Result selectCourseNodeById(CourseNodeBo courseNodeBo) {
        QueryWrapper<CourseNodeEntity> sectionQueryWrapper = new QueryWrapper<>();
        sectionQueryWrapper.lambda().eq(CourseNodeEntity::getId, courseNodeBo.getId()).eq(CourseNodeEntity::getIsDeleted, IsDeletedEnum.NO.getKey()).orderByAsc(CourseNodeEntity::getSortCode);
        CourseNodeEntity courseNodeEntities = courseNodeMapper.selectOne(sectionQueryWrapper);
        if (courseNodeEntities==null) return Result.error().message("查询结果为空");
        return Result.ok().data(courseNodeEntities);
    }
}
